var gulp = require('gulp');

var less = require('gulp-less');
var cleanCss = require('gulp-clean-css');
var autoprefixer = require('gulp-autoprefixer');

var uglify = require('gulp-uglify');

var concat = require('gulp-concat');
var rename = require('gulp-rename');
var clean = require('gulp-clean');
var sourcemaps = require('gulp-sourcemaps');
var plumber = require('gulp-plumber');

var imagemin = require('gulp-imagemin');
var pngquant = require('imagemin-pngquant');
var spritesmith = require('gulp.spritesmith');
var cache = require('gulp-cache');

var browserSync = require('browser-sync');
var reload = browserSync.reload;

gulp.task('clean', function() {
    return gulp.src('dist', {read: false})
        .pipe(clean())
});

// compile less files
gulp.task('less', function() {
    return gulp.src(['src/css/reset.less', 'src/css/global.less', 'src/css/**/*.less'])
        .pipe(plumber())
        // .pipe(sourcemaps.init())
        .pipe(concat('all.css'))
        .pipe(less())
        // .pipe(rename('all.min.css'))
        // .pipe(cleanCss())
        // .pipe(autoprefixer({
        //     browsers: ['> 1%', 'not ie <= 8']
        // }))
        // .pipe(sourcemaps.write())
        .pipe(gulp.dest('src/css'))
        .pipe(browserSync.stream());
});

gulp.task('css', ['less'], function() {
    return gulp.src('src/css/all.css')
        .pipe(concat('all.min.css'))
        .pipe(cleanCss())
        .pipe(gulp.dest('dist/css'));
});

gulp.task('js-optimize', function() {
    return gulp.src(['src/js/*.js', '!src/js/lib/*.js'])
        // .pipe(sourcemaps.init())
        .pipe(gulp.dest('dist/js/tmp'))
        .pipe(rename('all.min.js'))
        .pipe(uglify(function (err) {
            console.log(arguments)
        }))
        // .pipe(sourcemaps.write())
        .pipe(gulp.dest('dist/js'));
});

gulp.task('js', ['js-optimize'], function() {
    gulp.src('src/js/lib/')
        .pipe(gulp.dest('dist/js/lib'));
});

gulp.task('img', function() {
    return gulp.src(['src/img/**/*.{png,jpg,gif}'])
        .pipe(plumber())
        .pipe(cache(imagemin({
            progressive: true,
            use: [pngquant()]
        })))
        .pipe(gulp.dest('dist/img'));
});

gulp.task('index', function() {
    return gulp.src(['src/index.html'])
        .pipe(gulp.dest('dist/'))
});

gulp.task('pages', ['index'], function() {
    return gulp.src(['src/pages/*.html'])
        .pipe(gulp.dest('dist/pages'))
})

gulp.task('sprite', function() {
    return gulp.src('src/img/tmp/!(sprite.png|*.css)')
        .pipe(spritesmith({
            imgName: 'ico.png',
            cssName: 'sprite.css'
        }))
        .pipe(gulp.dest('src/img'));
});

gulp.task('reload', ['less'], reload);
gulp.task('dev', function() {
    browserSync({
        server: {
            baseDir: './'
        },
        startPath: '/src'
    });

    let watcher = gulp.watch([
        '*.html',
        '**/*.html',
        'css/**/*.css',
        'css/**/*.less',
        'js/**/*.js',
        'img/**/*.*'
    ], {
        cwd: './src'
    }, [
        'reload'
    ])

    watcher.on('change', function(event) {
        console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
    });
});

gulp.task('build', ['css', 'js', 'img', 'pages'], function() {
});